package com.sgl.retrofitdemo.utils;


import android.content.Context;
import android.content.SharedPreferences;

public class SharedPrefUtil {
    private SharedPreferences.Editor mEditor;
    private SharedPreferences mSharedPreferences;
    private static SharedPrefUtil mSharedPrefUtil;
    private static final String NAME = "YOHO_MARS";

    public static SharedPrefUtil instance(Context context) {
        if (mSharedPrefUtil == null) {
            mSharedPrefUtil = new SharedPrefUtil(context, NAME);
        }
        return mSharedPrefUtil;
    }

    public SharedPrefUtil(Context context, String name) {
        mSharedPreferences = context.getSharedPreferences(name, Context.MODE_PRIVATE);
        mEditor = mSharedPreferences.edit();
    }

    public boolean putString(String key, String value) {
        mEditor.putString(key, value);
        return mEditor.commit();
    }

    public void putStringWithoutCommit(String key, String value) {
        mEditor.putString(key, value);
    }

    public boolean putInt(String key, int value) {
        mEditor.putInt(key, value);
        return mEditor.commit();
    }

    public boolean putFloat(String key, float value) {
        mEditor.putFloat(key, value);
        return mEditor.commit();
    }

    public boolean putLong(String key, long value) {
        mEditor.putLong(key, value);
        return mEditor.commit();
    }

    public boolean putBoolean(String key, boolean value) {
        mEditor.putBoolean(key, value);
        return mEditor.commit();
    }

    public int getInt(String key, int defaultValue) {
        return mSharedPreferences.getInt(key, defaultValue);
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return mSharedPreferences.getBoolean(key, defaultValue);
    }

    public float getFloat(String key, float defaultValue) {
        return mSharedPreferences.getFloat(key, defaultValue);
    }

    public long getLong(String key, long defaultValue) {
        return mSharedPreferences.getLong(key, defaultValue);
    }

    public String getString(String key, String defaultValue) {
        return mSharedPreferences.getString(key, defaultValue);
    }

    public boolean removeKey(String key) {
        mEditor.remove(key);
        return mEditor.commit();
    }

    public boolean clear() {
        mEditor.clear();
        return mEditor.commit();
    }

    public boolean commit() {
        return mEditor.commit();
    }
}